﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using OSGeo.MapGuide;
using System.Collections.Specialized;

namespace NegevGas
{
    public class utilityfunctions : System.Web.UI.Page
    {
        static String s_basePath = "";

        public void InitializeWebTier()
        {
            // Initialize web tier with the site configuration file.  The config
            // file should be in the same directory as this script.
          //  MapGuideApi.MgInitializeWebTier(HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"] + "../mapserver2012/webconfig.ini");
           s_basePath = HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            MapGuideApi.MgInitializeWebTier(s_basePath + "webconfig.ini");
           //MapGuideApi.MgInitializeWebTier(@"C:\Program Files\Autodesk\Autodesk Infrastructure Web Server Extension 2012\www\webconfig.ini");
        }
        //void InitializeWebTier()
        //{
        //     Initialize web tier with the site configuration file.  The config
        //     file should be in the same directory as this script.
        //    MapGuideApi.MgInitializeWebTier(Request.ServerVariables["APPL_PHYSICAL_PATH"] + "../mapserver2012/webconfig.ini");
        //}
        public static void InitializeWebTiers()
        {
            // Initialize web tier with the site configuration file.  The config
            // file should be in the same directory as this script.
            s_basePath = HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            MapGuideApi.MgInitializeWebTier(s_basePath + "webconfig.ini");
        }

        public NameValueCollection GetRequestParameters()
        {
            if ("POST" == HttpContext.Current.Request.HttpMethod)
            {
                return HttpContext.Current.Request.Form;
            }
            else
            {
                return HttpContext.Current.Request.QueryString;
            }
        }

        public static String GetBasePath()
        {
            return s_basePath;
        }

        public static String GetParameter(NameValueCollection parameters, String name)
        {
            String strval = parameters[name];
            if (null == strval)
                return "";

            return strval.Trim();
        }

      public   static String GetFeaturePropertyValue(MgReader featureReader, String propName)
        {
            String value = "";
            int propertyType = featureReader.GetPropertyType(propName);
            switch (propertyType)
            {
                case MgPropertyType.Boolean:
                    value = featureReader.GetBoolean(propName).ToString();
                    break;
                case MgPropertyType.Byte:
                    value = featureReader.GetByte(propName).ToString();
                    break;
                case MgPropertyType.Single:
                    value = featureReader.GetSingle(propName).ToString();
                    break;
                case MgPropertyType.Double:
                    value = featureReader.GetDouble(propName).ToString();
                    break;
                case MgPropertyType.Int16:
                    value = featureReader.GetInt16(propName).ToString();
                    break;
                case MgPropertyType.Int32:
                    value = featureReader.GetInt32(propName).ToString();
                    break;
                case MgPropertyType.Int64:
                    value = featureReader.GetInt64(propName).ToString();
                    break;
                case MgPropertyType.String:
                    value = featureReader.GetString(propName);
                    break;
                case MgPropertyType.DateTime:
                case MgPropertyType.Null:
                case MgPropertyType.Blob:
                case MgPropertyType.Clob:
                case MgPropertyType.Feature:
                case MgPropertyType.Geometry:
                case MgPropertyType.Raster:
                    value = "[unsupported data type]";
                    break;
            }
            return value;
        }

        // Searches the map for a layer with the specified name.
        public static  MgLayer GetLayerByName(MgMap map, String layerName)
        {
            MgLayer layer = null;
            for (int i = 0; i < map.GetLayers().GetCount(); i++)
            {
                MgLayer nextLayer = (MgLayer)map.GetLayers().GetItem(i);
                if (nextLayer.GetName().Equals(layerName))
                {
                    layer = nextLayer;
                    break;
                }
            }
            return layer;
        }

        void OutputReaderContent(HttpResponse response, MgByteReader byteReader)
        {
            MemoryStream memBuf = new MemoryStream();

            byte[] byteBuffer = new byte[1024];
            int numBytes = byteReader.Read(byteBuffer, 1024);
            while (numBytes > 0)
            {
                memBuf.Write(byteBuffer, 0, numBytes);
                numBytes = byteReader.Read(byteBuffer, 1024);
            }

            response.ContentType = byteReader.GetMimeType();
            byte[] content = memBuf.ToArray();
            response.OutputStream.Write(content, 0, content.Length);
        }

    }
}